'''
Created on 06/09/2011

@author: Koos
'''
from datetime import datetime
from flaskext.sqlalchemy import SQLAlchemy
import app

db = SQLAlchemy(app.app)

class User(db.Model):
    
    __tablename__ = "users"
    
    ADMIN = 1
    MOD = 2
    MEMBER = 3
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True)
    email = db.Column(db.String(120), unique=True)
    votes = db.Column(db.Integer, default=0)
    password = db.Column(db.String(80))
    role = db.Column(db.Integer(1), default=3)
    active = db.Column(db.Integer(1), default=0)
    joined = db.Column(db.DateTime)
    
    posts = db.relationship("Post")
    
    def __init__(self, username, password, email, joined=None):
        self.username = username
        self.password = password
        self.email = email
        if joined is None:
            joined = datetime.utcnow()
            
    #def __repr__(self):
    #   data = self.username, self.password, self.email
    #  return 'User(%s, %s, %s)' % data

class Post(db.Model):
    
    __tablename__ = "posts"
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80))
    body = db.Column(db.Text)
    created = db.Column(db.DateTime)
    
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    
    tag_id = db.Column(db.Integer, db.ForeignKey("tags.id"))
    
    def __init__(self, title, body, user, tag, created=None):
        self.title = title
        self.body = body
        if created is None:
            created = datetime.utcnow()
        self.created = created
        self.user = user
        self.tag = tag

    def __repr__(self):
        return '<Post %r>' % self.title
    
    
class Tag(db.Model):
    
    __tablename__ = "tags"
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

    posts = db.relationship("Post")

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Tag %r>' % self.name